// doule_fpsDlg.cpp : implementation file
//

#include "stdafx.h"
#include "doule_fps.h"
#include "doule_fpsDlg.h"

#include "dbfps_alg.h"
//#include "alg1.cpp"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

//#define DEBUG_PRINT
FILE *fp_debug = NULL;

//Gloube Env Var
BOOL file_open_flag = FALSE;
CFile* src_yuv = NULL;
CFile* dst_yuv = NULL;	

//alg
extern int dbfps_alg1(yuv src_frm,yuv dst_frm,int pic_width,int pic_height);
extern int dbfps_alg2(yuv src_frm,yuv ref_frm,yuv dst_frm,int pic_width,int pic_height);
extern int dbfps_alg3(yuv src_frm,yuv ref_frm,yuv dst_frm,int pic_width,int pic_height);
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDoule_fpsDlg dialog


CDoule_fpsDlg::CDoule_fpsDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CDoule_fpsDlg::IDD, pParent)
{
	m_Pic_Width = 1600;
	m_Pic_Height = 1200;
	//{{AFX_DATA_INIT(CDoule_fpsDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

}

void CDoule_fpsDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDoule_fpsDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
	DDX_Control(pDX, IDC_ALG_SEL,m_AlgSel);
	DDX_Text(pDX, IDC_WIDTH_IN, m_Pic_Width);
	DDX_Text(pDX, IDC_HEIGHT_IN, m_Pic_Height);
	

}

BEGIN_MESSAGE_MAP(CDoule_fpsDlg, CDialog)
	//{{AFX_MSG_MAP(CDoule_fpsDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(ID_QUIT, OnQuit)
	ON_BN_CLICKED(ID_CONV, OnConv)
	ON_BN_CLICKED(IDC_OPEN, OnOpen)
	ON_EN_CHANGE(IDC_WIDTH_IN, OnChangeWidthIn)
	ON_EN_CHANGE(IDC_HEIGHT_IN, OnChangeHeightIn)
	ON_CBN_SELCHANGE(IDC_ALG_SEL, OnSelchangeAlgSel)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDoule_fpsDlg message handlers

BOOL CDoule_fpsDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	m_AlgSel.AddString("ALG1");
	m_AlgSel.InsertString(1,"ALG2");
	m_AlgSel.InsertString(2,"ALG3");
	
	m_AlgSel.SetCurSel(2);
	alg_select = 2;

	pic_height = 1200;
	pic_width  = 1600;
	
	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
#ifdef DEBUG_PRINT
	fp_debug = fopen("./debug_info.txt", "w+");
	if (fp_debug == NULL)
	{
		AfxMessageBox("Debug file open failed!");
	}
#else
#endif
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CDoule_fpsDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CDoule_fpsDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CDoule_fpsDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CDoule_fpsDlg::OnQuit() 
{
	// TODO: Add your control notification handler code here

	if(fp_debug)
	{
		fclose(fp_debug);
		fp_debug = NULL;
	}
	 exit(0);
}

void CDoule_fpsDlg::OnConv() 
{
	// TODO: Add your control notification handler code here
	// call the main function
	int frame_cnt = 0;
	int frm_size  = pic_height * pic_width;
	char frm_cnt_dsp[10] = {0};
	
	yuv src_frm;//1st
	yuv ref_frm;//2nd
	yuv dst_frm;//frm to be interp
	
	if(!file_open_flag)
	{
		AfxMessageBox("No file need convert!");
		return;
	}
	
	src_frm.Y  = (unsigned char*)malloc(frm_size);
	src_frm.CB = (unsigned char*)malloc(frm_size);
	src_frm.CR = (unsigned char*)malloc(frm_size);
	ref_frm.Y  = (unsigned char*)malloc(frm_size);
	ref_frm.CB = (unsigned char*)malloc(frm_size);
	ref_frm.CR = (unsigned char*)malloc(frm_size);
	dst_frm.Y  = (unsigned char*)malloc(frm_size);
	dst_frm.CB = (unsigned char*)malloc(frm_size);
	dst_frm.CR = (unsigned char*)malloc(frm_size);

	//begin convert
	SetDlgItemText(ID_CONV, "Converting... ");  

	//for test only
//	for(frame_cnt=0;frame_cnt<5;frame_cnt++)
//	{
//		char frm_cnt_dsp[10] = {0};
//		Sleep(1000);
//		sprintf(frm_cnt_dsp,"%d",frame_cnt);
//		SetDlgItemText(IDC_FRM_CNT, frm_cnt_dsp);
//	}

	//1st frm
	if(frm_size != src_yuv->Read(src_frm.Y,frm_size))
		goto free_mem;
	if(frm_size != src_yuv->Read(src_frm.CB,frm_size))
		goto free_mem;
	if(frm_size != src_yuv->Read(src_frm.CR,frm_size))
		goto free_mem;

	do{
		if(frm_size != src_yuv->Read(ref_frm.Y,frm_size))
			break;
		if(frm_size != src_yuv->Read(ref_frm.CB,frm_size))
			break;
		if(frm_size != src_yuv->Read(ref_frm.CR,frm_size))
			break;
		
		if(1)//display frame num
		{
			sprintf(frm_cnt_dsp,"%d",frame_cnt);
			SetDlgItemText(IDC_FRM_CNT, frm_cnt_dsp);
		}

		if(fp_debug)
			fprintf(fp_debug,"Frame %4d start\n",frame_cnt);

		switch(alg_select)
		{
			case 0://frame copy
				dbfps_alg1(src_frm,dst_frm,pic_width,pic_height);
				break;
			case 1://frame mid-interpolate
				dbfps_alg2(src_frm,ref_frm,dst_frm,pic_width,pic_height);
				break;
			case 2://me interpolate
				dbfps_alg3(src_frm,ref_frm,dst_frm,pic_width,pic_height);
				break;
			default:
				break;
		}
	

		dst_yuv->Write(src_frm.Y,frm_size);		
		dst_yuv->Write(src_frm.CB,frm_size);
		dst_yuv->Write(src_frm.CR,frm_size);
		dst_yuv->Write(dst_frm.Y,frm_size);
		dst_yuv->Write(dst_frm.CB,frm_size);
		dst_yuv->Write(dst_frm.CR,frm_size);
		
		memcpy(src_frm.Y,ref_frm.Y,frm_size);
		memcpy(src_frm.CB,ref_frm.CB,frm_size);
		memcpy(src_frm.CR,ref_frm.CR,frm_size);
		frame_cnt++;
	}while(1);


free_mem:
	free(src_frm.Y  );
	free(src_frm.CB );
	free(src_frm.CR );
	free(ref_frm.Y  );
	free(ref_frm.CB );
	free(ref_frm.CR );
	free(dst_frm.Y  );
	free(dst_frm.CB );
	free(dst_frm.CR );

	//end convert
	SetDlgItemText(ID_CONV, "Convert");  
	AfxMessageBox("Convert Finished!");
}

void CDoule_fpsDlg::OnOpen() 
{
	// TODO: Add your control notification handler code here

	char file_name[256]={0};
	char file_name_o[256]={0};

	if(file_open_flag)
	{
		if(src_yuv)
			src_yuv->Close();
		if(dst_yuv)
			dst_yuv->Close();
		
		SetDlgItemText(IDC_OPEN, "Open");  
		SetDlgItemText(IDC_FILE_DISP, "File Not Opened!");  
		m_AlgSel.EnableWindow(TRUE);
		file_open_flag = FALSE;
	}
	else
	{

		UpdateData(TRUE);
		//open file
		src_yuv = new CFile();
		dst_yuv = new CFile();
		
		char BASED_CODE szFilter[] = "All Files (*.*)|*.*||";
		CFileDialog dlg( TRUE, "yuv", NULL, OFN_HIDEREADONLY,szFilter);
		if(dlg.DoModal()!=IDOK) return ; 
		
		sprintf(file_name, "%s", dlg.GetPathName());
		sprintf(file_name_o, "%s.dpfps_%1d.yuv", dlg.GetPathName(),alg_select+1);
		
		
		if(!src_yuv->Open(file_name, CFile::modeRead )) 
		{
			AfxMessageBox("Can't open input file");
			return ;
		}

		if(!dst_yuv->Open(file_name_o, CFile::modeCreate|CFile::modeWrite )) 
		{
			src_yuv->Close();
			AfxMessageBox("Can't open output file");
			return ;
		}
		
		//open file successfully
		{
			m_AlgSel.EnableWindow(FALSE);
			SetDlgItemText(IDC_OPEN, "Close");  
			SetDlgItemText(IDC_FILE_DISP, file_name);  
			file_open_flag = TRUE;
		}	
	}

	
}


void CDoule_fpsDlg::OnChangeWidthIn() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	UpdateData(true);
	pic_width = m_Pic_Width;
}

void CDoule_fpsDlg::OnChangeHeightIn() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	UpdateData(true);
	pic_height = m_Pic_Height;
}

void CDoule_fpsDlg::OnSelchangeAlgSel() 
{
	// TODO: Add your control notification handler code here
	UpdateData(true);
	alg_select = m_AlgSel.GetCurSel();
}
